package com.cj.webapp_Start.sp_network.encryption;


import android.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public final class AESUtilsExt {


    private AESUtilsExt() {
    }

    private static String hexStr = "0123456789abcdef";  //全局

    /**
     * @param content    明文字符串
     * @param encryptKey 16位密钥字符串
     * @return 将明文加密成16进制字符串
     */
    public static String encryptToHex(String content, String encryptKey) {
        try {
            byte[] encrypted = aesEncryptToBytes(content, encryptKey);
            return Base64.encodeToString(encrypted, Base64.NO_WRAP);
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }

    /**
     * @param content    明文字符串
     * @param encryptKey 16位密钥字符串
     * @return 将明文加密成base64字符串
     */
    public static String encryptToBase64(String content, String encryptKey) {
        try {
            return base64Encode(aesEncryptToBytes(content, encryptKey));
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }

    /**
     * @param encryptContentHex 密文文字符串
     * @param decryptKey        16位密钥字符串
     * @return 将明文解密成16进制字符串
     */
    public static String decryptHex(String encryptContentHex, String decryptKey) {
        try {
            return aesDecryptByBytes(Base64.decode(encryptContentHex, Base64.NO_WRAP), decryptKey);
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }

    /**
     * @param encryptContentBase64 密文文字符串
     * @param decryptKey           16位密钥字符串
     * @return 将明文加密成16进制字符串
     */
    public static String decryptBase64(String encryptContentBase64, String decryptKey) {
        try {
            return aesDecryptByBytes(base64Decode(encryptContentBase64), decryptKey);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * @param bytes 16进制字符串
     * @return 字节数组转base64字符串
     */
    public static String base64Encode(byte[] bytes) {
        return Base64.encodeToString(bytes, Base64.NO_WRAP);
    }

    /**
     * @param base64Data 16进制字符串
     * @return base64字符串转字节数组
     */
    public static byte[] base64Decode(String base64Data) throws Exception {
        return Base64.decode(base64Data, Base64.NO_WRAP);
    }

    /**
     * @param content    明文字符串
     * @param encryptKey 16位密钥字符串
     * @return 将明文加密成bytes数组
     */
    private static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        byte[] keyBytes = encryptKey.getBytes();
        IvParameterSpec iv = new IvParameterSpec(keyBytes);
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, "AES"), iv);
        return cipher.doFinal(content.getBytes());
    }

    /**
     * @param encryptBytes 密文bytes数组
     * @param decryptKey   16位密钥字符串
     * @return 将密文数组解密成字符串
     */
    private static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        byte[] keyBytes = decryptKey.getBytes();
        IvParameterSpec iv = new IvParameterSpec(keyBytes);
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, "AES"), iv);
        byte[] decryptBytes = cipher.doFinal(encryptBytes);
        return new String(decryptBytes);
    }

    public static void main(String[] args) throws Exception {

//        String key = "3eDf&*sj7g1yS0pZ";
//        String a = "dasdkajsdhkashdkajshdkas大水库精华！@#!@#";
//        String aesA = encryptToHex(a, key);
//
//        System.out.println("result:" + aesA);
//
//        String encryptBase64 = "f753d537ec0261c49d46f4c30048c07822d0c1bb7c04176889db078c458c1a6b8f560538eb088e2d49ab463f7ca233e10286d9670324eec91999af2222f24e0e2f41df24ac8e03cbaf2117bfd5ce187bfa257d0e3d386648365e9b6f414bd09df9797c10031986e302c2c847a5a6db22588fdcc2efd983ea5eccaf13b101f82ab3c33c64678b15d180d3b272e192fb6008c9c1dd5381b891bc4e1836f5215ebc5a0f135b81be395a5c47805cb355f25925dd7a527d6a1c791c3e71ce6ca721de68982043c3cafc85f5d9a0787d6251acbca3680ba9833be8c4a49c758809ca28cb64bfab23b04b7eb4cf5dd53e9cfa40dca9a160a7ecbd317a2a7b72ef0f9465c36839c84aca41046b64f44de94ba835bf57525fddf949b8f004c22ef31800f93a3fe28351f472eed0635ccfa66a8c5d238eb933b7677b84f33e37b35ad568bbf9ca9989b076e7d744970c141115291c";
//        String c = decryptHex(encryptBase64, key);
//        System.out.println("result:" + c);
        String key = "yRNXDprTl5ZekhqiIi0GCjm6zNFArb82nKsnP32GPOgE69qVwPwF15/U0Azk4bE0f7Ftixv/rYGWfO/YqmGm32uQ2Irngn7+GQLNuntLWeWONpsZBiXwPXOmbflyTWMZrVt7ZtAqP3YCktHjQsHvY5m3vvfdET08hIrF2KSYLA==";
        String data = "CoAIkqHJEjO4HUss9I9r0bTuyFyJsl3+ibWgPDsiVmM=";
        String h5Private = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJoqZ2o55eHGqsEnXnele2TC+M7p/SxwfTjk67QwuNtDMO1mKBDc8teJfOsJimlmTNVsMwZMQgbAHLTS8oK3VI3os5uyxCeZ3PVHVQndqgL9X7yHT1NmjY0mmKDZVY6sZvmhHJ8yeIPSGWBxKEJfWy2vVSJjdrG8/MwkYWv2uHXhAgMBAAECgYAScUAt1P9LIxkKQktqxMEeLLTXC+bdihdw1vHbfHigwkj4HWBtpADpB+mJtHp6YWKPpYH1HupHJHRKrXGVcZkLsMg4+Xt9BHruxD9BqYsnZXnF66QbMz9K1XB13A/0mPj1DtJVts6kkZud/MlXfV3Q+uVyPTcC+de0kkmdc/hSgQJBAOjy5MD3AAZv32rwTnCGzlTyXFAG3E0ZQRR0eCUDnAPYd34oi5baUvBEec4l4Y7yvcHnzse4/EgVrIBjRxQ+Kd8CQQCpa8Jh+3C4Qqk34vjJtwcvLk9SH32msgJARToPCU5RYzWa4BNPMreqcivZGssstUqMkGyZWWRy/JiFv/sq6Ng/AkEAjgaRS8kMfeA1ckjDOj4mjIobmDsmk+ci06rTGZYiyAFxkTx9JuNDj/Pf470a0YHUCeYgH4r8XaLBQqHE02W3NwJADzvuVasrXpY36xuQrWq0zaxHW6YTQVr+UtMmvlVi4nSeTFfbxKJV25C6mqILJPEETBFsgnmES8ak3Fn/9sLLNwJAFOGkEFcI82vmiMpSDAq8ReWfLrThq0x6RFTDbTuLCsmtzJ1+H+ViFdmljsw9PWiTQZsaSaCGMlL59WkU7Svfcg==";

        byte[] aesBytes = Base64.decode(key, Base64.NO_WRAP);
        byte[] decodeByte = RSAUtilsExt.decryptByPrivateKey(aesBytes, h5Private);
        String decodeKey = new String(decodeByte);
        System.out.println("decodeKey===" + decodeKey);
        String decodeData = AESUtilsExt.decryptHex(data, decodeKey);
        System.out.println("decodeData===" + decodeData);
    }
}
